package Poly3;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:这是一个使用面向过程写法的零钱通系统。
 * User:lenovo
 * Date:2024-06-05
 * Time:14:34
 */
public class SmallChangeSys {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        boolean loop=true;

        //1.零钱通明细
        String key="";
        //通过字符串details拼接信息，再将它打印出来
        String details="--------------零钱通明细---------------";

        //2.收益入账
        double money=0;
        double balance=0;//余额
        //Date date=new Date();
        Date date=null;
        //SimpleDateFormat:格式化当前时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");

        //3.消费
        String note="";

        //4.退出
        String choice="";
        do{
            System.out.println("\n=============零钱通项目===========");
            System.out.println("\t\t\t1.零钱通明细");
            System.out.println("\t\t\t2.收益入账 ");
            System.out.println("\t\t\t3.消费    ");
            System.out.println("\t\t\t4.退出    ");

            System.out.println("请输入你的选择（1~4）：");
            key=scanner.next();
            //switch控制语句
            switch (key){
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.println("请输入入账金额：");
                    money=scanner.nextDouble();
                    //money的值需要校验，一会写
                    //思路：找出金额不符合的条件，而不是分成两部分，一部分金额正确，一部分金额不正确
                    while(true){
                        if(money<=0){
                            System.out.println("收入金额有误，请重新输入");
                            money=scanner.nextDouble();
                        }else
                            break;;
                    }
                    balance+=money;
                    date=new Date();
                    details+="\n收益入账：\t+"+money+"\t"+(sdf.format(date))+"\t"+balance;
                    break;
                case "3":
                    System.out.println("请输入消费金额：");
                    money=scanner.nextDouble();
                    //money的值需要校验，一会写
                    while(true){
                        if(money<=0||money>balance){
                            System.out.println("消费金额不正确，应该在0~"+balance+"请重新输入：");
                            money=scanner.nextDouble();
                        }else
                            break;
                    }
                    System.out.println("请输入消费说明：");
                    note=scanner.next();
                    balance-=money;
                    date=new Date();
                    details+="\n"+note+"\t\t-"+money+"\t"+(sdf.format(date))+"\t"+balance;
                    break;
                case "4":
                    while(true){
                        System.out.println("你确定要退出吗？y/n");
                        choice=scanner.next();
                        if(choice.equals("y")||choice.equals("n")){
                            break;
                        }
                    }
                    if(choice.equals("y"))
                        loop=false;
                    break;
                default:
                    System.out.println("选择有误，请重新选择！");
            }
        }while(loop);
        System.out.println("========项目结束！========");
    }
}
